<template>
  <div class="modify-dialog">
    <slot>
      <el-button size="small" type="primary" @click="showDialog()">
        新增供应商
      </el-button>
    </slot>
    <el-dialog v-bind="dialogOpts" :visible.sync="dialogVisible">
      <Form v-bind="formOpts" v-on="formEvent">
        <template #formBtn>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="onConfirm"> 确 定 </el-button>
          </div>
        </template>
      </Form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ProductInput from "./ProductInput.vue";
import modifyMixins from "@/mixins/modifyMixins.vue";
export default {
  mixins: [modifyMixins],
  components: {
    ProductInput,
  },
  data() {
    return {
      dialogWidth: "600px",
      form: {
        supplierName: {
          tag: "el-input",
          label: "供应商名称",
          value: "",
          props: {
            clearable: true,
            placeholder: "请输入供应商名称",
          },
        },
        bank: {
          tag: "el-input",
          label: "银行",
          value: "",
          props: {
            clearable: true,
            placeholder: "请输入银行",
          },
        },
        bankNumber: {
          tag: "el-input",
          label: "银行卡号",
          value: "",
          props: {
            clearable: true,
            placeholder: "请输入银行卡号",
          },
        },
        accountName: {
          tag: "el-input",
          label: "支行名称",
          value: "",
          props: {
            clearable: true,
            placeholder: "请输入支行名称",
          },
        },
        status: {
          tag: "el-select",
          label: "启用状态",
          value: 1,
          defValue: 1,
          items: [
            { label: "未启用", value: 0 },
            { label: "已启用", value: 1 },
          ],
          props: {},
        },
      },
    };
  },
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    showDialog(modify = "add", row = {}) {
      this.dialogVisible = true;
      this.row = row;
      this.modify = modify || "add";
      const keys = ["status"];
      for (const key in this.form) {
        let defValue = "";
        if (Array.isArray(this.form[key].value)) defValue = [];
        if (Object.hasOwnProperty.call(row, key))
          this.form[key].value = row[key];
        else this.form[key].value = defValue;
        this.form[key].hidden = false;
        if (modify == "add") this.form[key].hidden = keys.includes(key);
      }
      this.formKey = this.$quinn.guid();
    },
    onSubmit(form) {
      const { modify, row } = this;
      if (modify === "add")
        this.$api.supplierSupplierAdd(form).then(this.submitDoneMsg);
      else if (["edit"].includes(modify))
        this.$api
          .supplierSupplierUpdate({ ...form, supplierId: row.supplierId })
          .then(this.submitDoneMsg);
    },
  },
};
</script>

<style lang="less" scoped></style>
